package com.ipharma.interfaces.printer;

import java.io.*;

/**
 * A Printer class.
 * <P>
 * @author JorgE.H
 */
public class Printer
{
  /**
   * Attributes
   */
	public static String PORT = "COM1";
	public static String PORT_LOG = "printer.log";
	public static int LINE_WIDTH = 42;
  FileWriter spool;

  /**
   * Constructor
   * @param pPort port
	 * @param pWidth line width for printer
   */
  public Printer( String pPort, int pWidth ) throws IOException
  {
		LINE_WIDTH = pWidth;
		PORT = pPort;
    try
    {
   		spool = new FileWriter( PORT );
    }
    catch ( IOException ioe )
    {
      spool = new FileWriter( PORT_LOG, true );
    }
  }

  /**
   * Repeats character pChar, pTimes times.
   * @param pChar a string character
	 * @param pTimes number of times to repeat the character
   */
	public void repeat( String pChar, int pTimes ) throws IOException
	{
    String ls_line = "";
  	for ( int i = 0; i < LINE_WIDTH; i++ )
		{
			ls_line += pChar;
		}

    println( ls_line );
	}

  /**
   * Prints a character line
   * @param pText the string to be used as the line character
   */
	public void line( String pChar ) throws IOException
	{
		repeat( pChar, LINE_WIDTH );
	}

  /**
   * Advances one line
   */
	public void feedln() throws IOException
	{
		println( "" );
	}

  /**
   * Prints a full line
   * @param pText the string to print out
   */
	public void println( String pText ) throws IOException
	{
		print( pText + "\n" );
	}

  /**
   * Prints a line
   * @param pText the string to print out
   */
	public void print( String pText ) throws IOException
	{
 		spool.write( pText );
	}

  /**
   * Closes the output stream
   */
	public void close() throws IOException
	{
 		spool.close();
	}

  static public void main( String[] args )
  {
    try
    {
      Printer p = new Printer( "COM1", 42 );
      p.repeat( "*", 42 );
      p.feedln();
      p.println( "HOLA MUNDO" );
      p.repeat( "*", 42 );
      p.feedln();
      p.feedln();
      p.feedln();
      p.close();
    }
    catch ( IOException ioe )
    {
      ioe.printStackTrace();
    }
  }

}